package com.fone.player.view;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.personal.LoginActivity;
import com.fone.player.sns.bean.ShareInfo;
import com.fone.player.util.ScreenUtil;

public class UserListPopwindow implements OnItemClickListener {

	private static final String TAG = "UserListPopwindow";
	private PopupWindow mPopupWindow;
	private int height;
	private int width;
	private View popupWindowView;
	private List<String> userList;
	private LoginActivity mActivity;
	
	private ListView mListView;
	
	public UserListPopwindow(LoginActivity activity,int width,List<String> userList){
		mActivity = activity;
		this.width = width;
		this.userList = userList;
		if(userList.size()<3){
			height = ScreenUtil.dp2px(userList.size()*35+20);
		}else{
			height = ScreenUtil.dp2px(2*35+20);
		}
		
		LayoutInflater layoutInflater = (LayoutInflater) mActivity.getApplicationContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		popupWindowView = layoutInflater.inflate(R.layout.login_userlist_popwindow, null);
		mPopupWindow = new PopupWindow(popupWindowView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		
		//TODO 设置动画效果
		mPopupWindow.setAnimationStyle(R.style.anim_login_userlist);
		mPopupWindow.setWidth(this.width);
		mPopupWindow.setHeight(this.height);
		
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				if(mActivity.loginUserlistCb!=null&&mActivity.loginUserlistCb.isChecked()){
					mActivity.loginUserlistCb.setChecked(false);
				}
			}
		});
		
		iniView();
	}
	
	private void iniView(){
		mListView = (ListView) popupWindowView.findViewById(R.id.login_userlist_lv);
		mListView.setAdapter(new MyAdapter());
		mListView.setOnItemClickListener(this);
	}
	
	/**
	 * 弹出popwindow
	 * @param view	以这个布局的右下角为基准
	 * @param shareInfo 分享信息
	 */
	public void show(View view,int offsetX){
		if(mPopupWindow!=null){
			mPopupWindow.showAsDropDown(view, offsetX, 0);
		}
	}
	
	/**
	 * 隐藏popwindow
	 */
	public void dismiss() {
		if (mPopupWindow != null && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
	}
	
	public PopupWindow getPopupWindow() {
		return mPopupWindow;
	}
	
	public boolean isShowing(){
		if (mPopupWindow != null) {
			return mPopupWindow.isShowing();
		}
		return false;
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
		mActivity.usernameEt.setText(userList.get(position));
		mActivity.usernameEt.setSelection(userList.get(position).length());
		dismiss();
	}
	
	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return userList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		ViewHolder viewHolder;
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			if(convertView==null){
				convertView = LayoutInflater.from(mActivity.getApplicationContext()).inflate(
						R.layout.login_userlist_item, null);
				viewHolder = new ViewHolder();
				viewHolder.usernameTv=(TextView) convertView.findViewById(R.id.login_userlist_item_tv);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.usernameTv.setText(userList.get(position));
			return convertView;
		}
		
		private class ViewHolder {
			public TextView usernameTv;
		}
		
	}

}
